package com.yj.lottery_system.controller;

import com.yj.lottery_system.common.errorcode.ControllerErrorCodeConstants;
import com.yj.lottery_system.common.exception.ControllerException;
import com.yj.lottery_system.common.pojo.CommonResult;
import com.yj.lottery_system.common.utils.JacksonUtil;
import com.yj.lottery_system.controller.param.CreateActivityParam;
import com.yj.lottery_system.controller.param.PageParam;
import com.yj.lottery_system.controller.result.CreateActivityResult;
import com.yj.lottery_system.controller.result.FindActivityListResult;
import com.yj.lottery_system.controller.result.GetActivityDetailResult;
import com.yj.lottery_system.service.IActivityService;
import com.yj.lottery_system.service.dto.ActivityDTO;
import com.yj.lottery_system.service.dto.ActivityDetailDTO;
import com.yj.lottery_system.service.dto.CreateActivityDTO;
import com.yj.lottery_system.service.dto.PageListDTO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.stream.Collectors;

@RestController

public class ActivityController {
    private static final Logger log = LoggerFactory.getLogger(ActivityController.class);
    @Resource
    private IActivityService activityService;

    /**
     * 创建活动
     * @param param
     * @return
     */
    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActivity (@RequestBody @Validated CreateActivityParam param) {
        //日志打印
        log.info("createActivity CreateActivityParam: {}", JacksonUtil.writeValueAsString(param));
        //调用service服务
        CreateActivityDTO activityDTO  = activityService.createActivity(param);
        return CommonResult.success(
                convertTOCreateActivityResult(activityDTO));

    }

    private CreateActivityResult convertTOCreateActivityResult(CreateActivityDTO activityDTO) {
        if(null == activityDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.CREATE_ACTIVITY_ERROR);
        }
        CreateActivityResult createActivityResult = new CreateActivityResult();
        createActivityResult.setActivityId(activityDTO.getActivityId());
        return createActivityResult;
    }

    /**
     * 查询活动列表
     * @param param
     * @return
     */

    @RequestMapping("/activity/find-list")
    public CommonResult<FindActivityListResult> findActivityList(PageParam param) {
        //日志打印
        log.info("findActivityList PageParam: {}", JacksonUtil.writeValueAsString(param));
        //调用service服务
        return CommonResult.success(convertTOFindActivityListResult( activityService.findActivityList(param)));
    }

    private FindActivityListResult convertTOFindActivityListResult(PageListDTO<ActivityDTO> activityList) {
        if(null == activityList) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_ACTIVITY_LIST_ERROR);
        }
        FindActivityListResult findActivityListResult = new FindActivityListResult();
        findActivityListResult.setTotal(activityList.getTotal());
        findActivityListResult.setRecords(activityList.getRecords().stream()
                .map( activityDTO -> {
                    FindActivityListResult.ActivityInfo activityInfo = new FindActivityListResult.ActivityInfo();
                    activityInfo.setActivityId(activityDTO.getActivityId());
                    activityInfo.setActivityName(activityDTO.getActivityName());
                    activityInfo.setDescription(activityDTO.getDescription());
                    activityInfo.setValid(activityDTO.valid());
                    return activityInfo;
                }).collect(Collectors.toList())
        );
        return findActivityListResult;
    }

    @RequestMapping("/activity-detail/find")
    public CommonResult<GetActivityDetailResult> getActivityDetailFind (Long activityId) {
        //日志打印
        log.info("getActivityDetailFind activityId: {}", JacksonUtil.writeValueAsString(activityId));
        //调用service服务
        ActivityDetailDTO activityDetailDTO = activityService.getActivityDetailFind(activityId);
        return CommonResult.success(
                convertTOGetActivityDetailResult(activityDetailDTO));
    }

    private GetActivityDetailResult convertTOGetActivityDetailResult(ActivityDetailDTO activityDetailDTO) {

        if(null == activityDetailDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAILS_ERROR);
        }
        GetActivityDetailResult getActivityDetailResult = new GetActivityDetailResult();
        getActivityDetailResult.setActivityId(activityDetailDTO.getActivityId());
        getActivityDetailResult.setActivityName(activityDetailDTO.getActivityName());
        getActivityDetailResult.setDescription(activityDetailDTO.getDesc());
        getActivityDetailResult.setValid(activityDetailDTO.valid());
        getActivityDetailResult.setPrizes(activityDetailDTO.getPrizeDTOList().stream()
                //按照奖品等级排序
                .sorted(Comparator.comparingInt(prizeDTO -> prizeDTO.getTires().getCode()))
                .map(prizeDTO -> {
                    GetActivityDetailResult.Prize prize = new GetActivityDetailResult.Prize();
                    prize.setPrizeId(prizeDTO.getPrizeId());
                    prize.setDescription(prizeDTO.getDescription());
                    prize.setImageUrl(prizeDTO.getImageUrl());
                    prize.setName(prizeDTO.getName());
                    prize.setPrice(prizeDTO.getPrice());
                    prize.setPrizeTierName(prizeDTO.getTires().getMessage());
                    prize.setPrizeAmount(prizeDTO.getPrizeAmount());
                    prize.setValid(prizeDTO.valid());
                    return prize;
                }).collect(Collectors.toList())
        );

        getActivityDetailResult.setUsers(activityDetailDTO.getUserDTOList().stream()
                .map(userDTO -> {
                    GetActivityDetailResult.User user = new GetActivityDetailResult.User();
                    user.setUserId(userDTO.getUserId());
                    user.setUserName(userDTO.getUserName());
                    user.setValid(userDTO.valid());

                    return user;
                }).collect(Collectors.toList())
        );

        return getActivityDetailResult;

    }
}
